# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and setting
import requests
import json
import excel_wr
import time
import random


class CusDict(dict):
    __setattr__ = dict.__setitem__
    __getattr__ = dict.__getitem__


def print_hi(name):
    print("==============print_hi============")
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


def return_value(name):
    print("==============return_value============")
    return name + "======="


# request baidu
def req_bai_du():
    print("==============req_bai_du============")
    result = requests.get("https://www.baidu.com")
    print(result.content.decode("utf-8"))


# json/dict convert string
def json_convert_str():
    print("==============json_convert_str============")
    json_obj = {"name": "suzt", "age": 18}
    print(type(json_obj))
    json_str = json.dumps(json_obj)
    print(type(json_str))
    print(json_str)


# string convert json/dict
def str_convert_json():
    print("==============str_convert_json============")
    json_str = '{"name": "suzt", "age": 18}'
    print(type(json_str))
    json_obj = json.loads(json_str)
    print(type(json_obj))
    print(json_obj)


# request请求并解析返回的json
def request_and_analyze():
    print("==============request_and_analyze============")
    url = "https://test-xy.deepblueai.com/api/app/pub/captcha/signin"
    body = {"captcha": "32432", "mobile": "15800987654"}
    headers = {'Content-Type': 'application/json'}
    result = requests.post(url, data=json.dumps(body), headers=headers)
    json_result = result.json()
    print(json_result)
    print(json_result["success"])
    print(json_result.get("success"))
    # 用自定义类，用.号取值
    json_result = CusDict(json_result)
    print(json_result.success)


def request_from_local_url():
    print("==============request_from_local_url============")
    url = "http://localhost:5000/json?param=sdfdsf"
    result = requests.post(url)
    print(result.content.decode("utf8"))


def request_from_local_json():
    print("==============request_from_local_json============")
    url = "http://localhost:5000/jsonPost"
    body = {"captcha": "32432", "mobile": "15800987654"}
    result = requests.post(url, data=json.dumps(body))
    json_result = result.json()
    print(json_result)


def read_from_excel():
    path = "snCodeBind.xlsx"
    sheet_name = "sn_test"
    list = excel_wr.read_excel(path, sheet_name, cus_row=2)
    # 遍历所有的
    for r in list:
        row_json_str = json.dumps(r, ensure_ascii=False)
        print(row_json_str)
    # 指定第一个
    row_json_str = json.dumps(list[0], ensure_ascii=False)
    print(row_json_str)
    # 拼接请求
    url = 'http://192.168.77.110:9456/rpc.json?_service=http://service.huifu.com/nsposmas/fposBankMerRegService_1.0.0&_method=register&_param={"req":' + row_json_str + '}'
    print(url)


def read_excel_list():
    path = "snCodeBind.xlsx"
    sheet_name = "sn_test"
    row_list = excel_wr.read_excel(path, sheet_name, cus_row=-1)
    return row_list


def assert_test():
    json_test = {"name": "suzt", "age": 18}
    assert json_test["name"] == "futl"
    print(json)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # print_hi('PyCharm')
    # value = return_value("pycharm")
    # print(value)
    # req_bai_du()
    # json_convert_str()
    # str_convert_json()
    # request_and_analyze()
    # request_from_local_url()
    # request_from_local_json()
    # read_from_excel()
    # assert_test()
    row_list = read_excel_list()
    cus_range = "1,4-5,7"
    real_list = excel_wr.filter_list_row(cus_range, row_list)
    index = 0
    for row in real_list:
        if row["AUTOMATION_FLAG"]=="N":
            continue
        index = index + 1
        if row["I_terminalModel"] == "自动获取":
            row["I_terminalModel"] = time.strftime("%Y%m%d%H%M%S")+str(index)
        if row["I_snCode"] == "自动获取":
           row["I_snCode"]="123456789012345678901234567890123456789012345678"
        if row["I_posCompany"]=="自动获取":
            row["I_posCompany"]=time.strftime("%Y%m%d%H%M%S")+str(random.random()*100000000).split(".")[0]
        print(row)
